﻿using System;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace JotaControls
{
    [ToolboxData("<{0}:CustomFeedback runat=server></{0}:CustomFeedback>")]
    [ToolboxBitmap(typeof(CustomFeedback), "Imgs.message.ico")]
    public class CustomFeedback : WebControl
    {
        #region Properties
        private Table Tbl;
        private TableRow Tr;
        private TableCell CellImg;
        private TableCell CellText;
        private System.Web.UI.WebControls.Image FeedBackImage;
        private Label FeedBackLabel;
        private string FeedbackText { get; set; }
        private bool FeedbackVisible { get; set; }
        private AlertTypeCss FeedbackAlertType { get; set; }
        public enum AlertTypeCss
        {
            Sucess = 0,
            Error = 1,
            Warning = 2
        }
        #endregion

        #region Methods
        public void SetFeedback(string Text, AlertTypeCss AlertType)
        {
            FeedbackText = Text;
            FeedbackAlertType = AlertType;
            FeedbackVisible = true;
        }
        private string SetCss()
        {
            string CssClass = string.Empty;
            switch ((int)FeedbackAlertType)
            {
                case 0:
                    CssClass = "SuccessTD";
                    break;
                case 1:
                    CssClass = "ErrorTD";
                    break;
                case 2:
                    CssClass = "AlertTD";
                    break;
                default:
                    CssClass = "AlertTD";
                    break;
            }
            return CssClass;
        }
        private string SetFeedbackIcon()
        {
            string IconUrl = string.Empty;
            switch ((int)FeedbackAlertType)
            {
                case 0:
                    IconUrl = "JotaControls.Imgs.icon_ok.png";
                    break;
                case 1:
                    IconUrl = "JotaControls.Imgs.icon_error.png";
                    break;
                case 2:
                    IconUrl = "JotaControls.Imgs.icon_alert.png";
                    break;
                default:
                    IconUrl = "JotaControls.Imgs.icon_alert.png";
                    break;
            }
            return IconUrl;
        }
        private void RenderCSS()
        {
            if (!DesignMode && Page.Header != null)
            {
                bool linkIncluded = false;
                foreach (Control c in Page.Header.Controls)
                {
                    if (c.ID == "CustomButtonStyle")
                    {
                        linkIncluded = true;
                    }
                }
                if (!linkIncluded)
                {
                    HtmlGenericControl csslink = new HtmlGenericControl("link");
                    csslink.ID = "CustomButtonStyle";
                    csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(typeof(CustomFeedback), "JotaControls.Css.Style.css"));
                    csslink.Attributes.Add("type", "text/css");
                    csslink.Attributes.Add("rel", "stylesheet");
                    Page.Header.Controls.Add(csslink);
                }
            }
        }
        #endregion

        #region Events
        protected override void CreateChildControls()
        {
            Tbl = new Table();
            Tr = new TableRow();
            CellImg = new TableCell();
            CellText = new TableCell();
            FeedBackImage = new System.Web.UI.WebControls.Image();
            FeedBackLabel = new Label();
            Tbl.Width = new Unit(100, UnitType.Percentage);
            Tbl.CellPadding = 0;
            Tbl.CellSpacing = 0;
            Tbl.CssClass = SetCss();
            Tbl.Visible = FeedbackVisible;
            FeedBackImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(CustomFeedback), SetFeedbackIcon());
            FeedBackLabel.Text = FeedbackText;
            CellImg.Width = new Unit(20, UnitType.Pixel);
            CellImg.Controls.Add(FeedBackImage);
            CellText.Controls.Add(FeedBackLabel);
            Tr.Height = new Unit(20, UnitType.Pixel);
            Tr.Controls.Add(CellImg);
            Tr.Controls.Add(CellText);
            Tbl.Controls.Add(Tr);
            Controls.Add(Tbl);
            base.CreateChildControls();
        }
        protected override void OnPreRender(EventArgs e)
        {
            RenderCSS();
        }
        protected override void Render(HtmlTextWriter output)
        {
            this.RenderContents(output);
        }
        #endregion
    }
}
